//
//  ZPPoppingBaseView.m
//  ZPIOSEducation
//
//  Created by mini1 on 2016/12/20.
//  Copyright © 2016年 mini1. All rights reserved.
//

#import "ZPPoppingBaseView.h"

@interface ZPPoppingBaseView ()
{
    UIControl *_bgControl;
}
@end

@implementation ZPPoppingBaseView

//- (void)dealloc
//{
//    [_bgControl release];
//}


+ (id)viewFremNib
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] objectAtIndex:0];
}

+ (id)loadFromNib
{
    return [self viewFremNib];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    _bgControl = [[UIControl alloc] init];
//    _bgControl.backgroundColor = black_color;
    _bgControl.alpha = 0.5f;
    [_bgControl addTarget:self action:@selector(onCancelBtn:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)show
{
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    _bgControl.frame = window.bounds;
    self.center = window.center;
    [window addSubview:_bgControl];
    [window addSubview:self];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1.0;
        [self.layer addAnimation:[ZPPoppingBaseView scaleAnimation:YES] forKey:@"ZPPOPPINGVIEWWILLAPPEAR"];
    } completion:^(BOOL finished) {
        if (finished) {
            [self didShow];
        }
    }];
}


- (IBAction)onCancelBtn:(id)sender
{
    [self willCancel:sender?YES:NO];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0;
        [self.layer addAnimation:[ZPPoppingBaseView scaleAnimation:NO] forKey:@"ZPPOPPINGVIEWWILLAPPEAR"];
    } completion:^(BOOL finished) {
        if (finished) {
            [self didCancel];
            [_bgControl removeFromSuperview];
            [self removeFromSuperview];
        }
        
    }];
}


- (void)cancel
{
    [self onCancelBtn:nil];
}


- (void)cancelWithAnimation:(BOOL)isAnimation
{
    if (isAnimation) {
        [self onCancelBtn:nil];
    } else {
        [_bgControl removeFromSuperview];
        [self removeFromSuperview];
    }
}


#pragma mark - <Subclass>
- (void)didShow {}
- (void)willCancel:(BOOL)isAutoCancel {}
- (void)didCancel {}


#pragma mark - <Animation>
+ (CAKeyframeAnimation *)scaleAnimation:(BOOL)show
{
    CAKeyframeAnimation *scaleAnimation = nil;
    scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    scaleAnimation.delegate = self;
    scaleAnimation.fillMode = kCAFillModeForwards;
    
    NSMutableArray *values = [NSMutableArray array];
    if (show) {
        scaleAnimation.duration = 1;
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    } else {
        scaleAnimation.duration = 1;
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.6, 0.6, 1.0)]];
    }
    scaleAnimation.values = values;
    scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    scaleAnimation.removedOnCompletion = TRUE;
    return scaleAnimation;
}
@end
